const router = require('koa-router')();
const indexService = require('../services/index');

const msgModule = require('../common/msg');
const Result = require('../common/Result');

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    navList: ["首页", "菜单管理", "系统设置"]
  })
})

router.post('/login', async (ctx, next) => {
  let param = ctx.request.body;
  try {
    const result = await indexService.login(param);
    ctx.body = Result.success(result, msgModule.loginSuccess);
  } catch (error) {
    ctx.body = Result.error({
      ...error
    }, msgModule.loginError);
  }
});


//验证sessionkey过期没有
router.post('/checkSessionKey', async (ctx, next) => {
  let param = ctx.request.body;
  try {
    const result = await indexService.checkSessionKey(param);
    if (result == 0) {
      ctx.body = Result.success({}, msgModule.querySuccess);
    } else {
      ctx.body = Result.fail({}, msgModule.querySuccess,result);
    }
  } catch (error) {
    ctx.body = Result.error(error, msgModule.queryError);
  }
});

// const fs = require("fs");
// const path = require("path");

// //web文件上传
// router.post('/webupload', async (ctx, next) => {
//   let filePath = ctx.request.files.file.filepath;
//   let type = ctx.request.files.file.originalFilename.split(".");
//   const filepath = path.resolve(path.join(__dirname + "/../public/web/"), `${new Date().getTime()}.${type[type.length - 1]}`);
//   const readStream = fs.createReadStream(filePath);
//   const writeStream = fs.createWriteStream(filepath);
//   readStream.pipe(writeStream);
//   let bool = true;
//   readStream.on('end', function () {
//     bool = true;
//   })
//   readStream.on('err', function () {
//     bool = false;
//   })
//   ctx.body = {
//     title: "web上传图片",
//     code: bool ? 200 : 500,
//     msg: bool ? "上传成功" : "上传失败"
//   }

// })

// //富文本文件上传
// router.post('/wangEditor/webupload', async (ctx, next) => {
//   let filePath = ctx.request.files.file.filepath;
//   let type = ctx.request.files.file.originalFilename.split(".");
//   let name = `${new Date().getTime()}.${type[type.length - 1]}`;
//   const filepath = path.resolve(path.join(__dirname + "/../public/web/"), name);
//   const readStream = fs.createReadStream(filePath);
//   const writeStream = fs.createWriteStream(filepath);
//   readStream.pipe(writeStream);
//   let bool = true;
//   readStream.on('end', function () {
//     bool = true;
//   })
//   readStream.on('err', function () {
//     bool = false;
//   })
//   ctx.body = {
//     "errno": 0, // 注意：值是数字，不能是字符串
//     "data": {
//       "url": "http://192.168.1.3:3001/web/" + name, // 图片 src ，必须
//       "alt": name, // 图片描述文字，非必须
//       "href": "http://192.168.1.3:3001/web/" + name // 图片的链接，非必须
//     }
//   }

// })

// //uni文件上传
// router.post('/uniupload', async (ctx, next) => {
//   //uview 的上传方式
//   let filePath = ctx.request.files.file.filepath;
//   let time = new Date().getTime();
//   let type = ctx.request.files.file.originalFilename.split(".");
//   fs.writeFileSync(path.join(__dirname + "/../public/app/" + `${time}.${type[type.length - 1]}`), fs.readFileSync(filePath))
//   ctx.body = {
//     code: 200,
//     msg: "上传成功",
//     data: time + type
//   }
// })

module.exports = router